package com.guohao.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guohao.reggie.common.R;
import com.guohao.reggie.dto.DishDto;
import com.guohao.reggie.dto.SetmealDto;
import com.guohao.reggie.entity.Dish;
import com.guohao.reggie.entity.Setmeal;
import com.guohao.reggie.service.SetmealDishService;
import com.guohao.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@RequestMapping("/setmeal")
@RestController
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {

        log.info("套餐信息: {}", setmealDto);
        setmealService.saveWithDish(setmealDto);

        return R.success("套餐保存成功");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {

        log.info("setmealDto :{}", name);
        //分页控制器
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);

        Page<SetmealDto> setmealDishPage = new Page<>(page, pageSize);

        // 条件构造器
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();

        //添加过滤条件
        setmealLambdaQueryWrapper.like(name != null, Setmeal::getName, name);

        // 添加过滤条件
        setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);

        // 查询
        setmealService.page(setmealPage, setmealLambdaQueryWrapper);
        //拷贝
        BeanUtils.copyProperties(setmealPage, setmealDishPage, "records");

        List<Setmeal> records = setmealPage.getRecords();

        List<SetmealDto> setmealDtos = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item, setmealDto);

            //分类ID
            Long categoryId = item.getCategoryId();
            //根据分类ID 查询到对象  获得分类名称
            Setmeal setmeal = setmealService.getById(categoryId);

            if (setmeal != null) {
                // 分类名称
                String setmealName = setmeal.getName();
                setmealDto.setCategoryName(setmealName);
            }
            return setmealDto;
        }).collect(Collectors.toList());


        setmealDishPage.setRecords(setmealDtos);

        return R.success(setmealDishPage);
    }

    /**
     * 根据Id进行删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteById(@RequestParam List<Long> ids) {
        log.info("删除套餐 : id位:{}", ids.toString());

        //调用 重写remove方法 (Service 重写)
        setmealService.remove(ids);
        return R.success("删除菜品成功!");
    }

    /**
     * 更新 禁卖 状态
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {

        // 创建 UpdateWrapper
        LambdaUpdateWrapper<Setmeal> dishLambdaUpdateWrapper = new LambdaUpdateWrapper<>();

        dishLambdaUpdateWrapper.set(Setmeal::getStatus, status);

        dishLambdaUpdateWrapper.in(Setmeal::getId, ids);

        setmealService.update(dishLambdaUpdateWrapper);

        return R.success("状态更新成功!");
    }

    /**
     * 根据id 查询菜品信息和对应的分类信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id) {

        SetmealDto SetmealDto = setmealService.getByIdWithSetmeal(id);
        return R.success(SetmealDto);
    }

    /**
     * 更新套餐
     *
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        log.info("setmealDto :{}", setmealDto);

        setmealService.updateWithDish(setmealDto);

        return R.success("更新套餐成功");
    }


    /**
     * 根据条件 查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        setmealLambdaQueryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        setmealLambdaQueryWrapper.orderByAsc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(setmealLambdaQueryWrapper);


        return R.success(list);
    }
}
